﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;

namespace IntellectShop.Common
{
	public partial class Schedule : SortedList<DateTime, ScheduleItem>
	{
		public static Schedule RetrieveByCourseID(int courseID)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var query =
							from scheduleItems in dataContext.ScheduleItems
							where scheduleItems.CourseID == courseID
							select scheduleItems;

				Schedule result = new Schedule(courseID);
				foreach (var row in query)
				{
					ScheduleItem item = new ScheduleItem
						{
							CourseID = courseID,
							EventDate = row.EventDate.Date,
							DurationHours = row.DurationHours,
							ScheduleItemID = row.ScheduleItemID,
							Note = row.Note
						};

					if (!result.ContainsKey(row.EventDate.Date))
					{

						result.Add(row.EventDate.Date, item);
					}
					else
					{
						result[row.EventDate.Date] = item;
					}
				}

				return result;
			}
		}

		public static Schedule RetrieveByCourseIDAndPeriod(int courseID, DateTime periodBegin)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var query =
							from scheduleItems in dataContext.ScheduleItems
							where
								scheduleItems.CourseID == courseID &&
								scheduleItems.EventDate.Date >= periodBegin.Date
							select scheduleItems;

				Schedule result = new Schedule(courseID);
				foreach (var row in query)
				{
					ScheduleItem item = new ScheduleItem
					{
						CourseID = courseID,
						EventDate = row.EventDate.Date,
						DurationHours = row.DurationHours,
						ScheduleItemID = row.ScheduleItemID,
						Note = row.Note
					};

					if (!result.ContainsKey(row.EventDate.Date))
					{

						result.Add(row.EventDate.Date, item);
					}
					else
					{
						result[row.EventDate.Date] = item;
					}
				}

				return result;
			}
		}

		public void Save()
		{
			using (TransactionScope trans = new TransactionScope())
			{
				using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
				{
					var query =
								from items in dataContext.ScheduleItems
								where items.CourseID == this.CourseID
								select items;
					dataContext.ScheduleItems.DeleteAllOnSubmit(query);
					dataContext.SubmitChanges();

					foreach (KeyValuePair<DateTime, ScheduleItem> kvp in this)
					{
						var scheduleItemDB = new DB.ScheduleItem
						{
							CourseID = this.CourseID,
							EventDate = kvp.Key.Date,
							DurationHours = kvp.Value.DurationHours,
							Note = kvp.Value.Note
						};

						dataContext.ScheduleItems.InsertOnSubmit(scheduleItemDB);
					}

					dataContext.SubmitChanges();

					trans.Complete();
				}
			}
		}
	}
}
